﻿///////////////////////////////////////////////////////////////////////////////
//
//  This file is part of MathLib.NET.
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//  
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//  
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library;  If not, see 
//  <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MathLib;
using ILNumerics;

namespace MathLibTest
{
    /// <summary>
    ///  functionality not provided by NUnit
    /// </summary>
    class TestUtils
    {
        static double toleranse = 0.000001;
        static bool console_out = true;

        /// <summary>
        /// Compares arrays of ILArray<int>
        /// </summary>
        /// <param name="a1">first array to compare</param>
        /// <param name="a2">second array to compare</param>
        /// <returns>true if arrays are equal, false in other case</returns>
        public static bool ArrayEqual(ILArray<int>[] a1, ILArray<int>[] a2)
        {
            if (a1 == null)
            {
                if (console_out)
                {
                    Console.WriteLine("ArrayEqual fault: first argument is null");
                }
                return false;
            }

            if (a2 == null)
            {
                if (console_out)
                {
                    Console.WriteLine("ArrayEqual fault: second argument is null");
                }
                return false;
            }

            if (a1.Length != a2.Length)
            {
                if (console_out)
                {
                    Console.WriteLine("ArrayEqual fault: different lengths");
                    Console.WriteLine("  arg1 lenght = {0:D}", a1.Length);
                    Console.WriteLine("  arg2 lenght = {0:D}", a2.Length);
                }
                return false;
            }

            for (int i = 0; i < a1.Length; ++i)
                if (!a1[i].Equals(a2[i]))
                {
                    if (console_out)
                    {
                        Console.WriteLine("ArrayEqual fault: different value at index {0:D}", i);
                        Console.WriteLine("  arg1[{0:D}]:", i);
                        Console.WriteLine(a1[i]);
                        Console.WriteLine("  arg2[{0:D}]:", i);
                        Console.WriteLine(a2[i]);
                    }
                    return false;
                }

            return true;
        }

        /// <summary>
        /// Compares arrays of double
        /// </summary>
        /// <param name="a1">first array to compare</param>
        /// <param name="a2">second array to compare</param>
        /// <returns>true if arrays are equal, false in other case</returns>
        public static bool ArrayEqual(double[] a1, double[] a2)
        {
            if (a1 == null)
            {
                if (console_out)
                {
                    Console.WriteLine("ArrayEqual fault: first argument is null");
                }
                return false;
            }

            if (a2 == null)
            {
                if (console_out)
                {
                    Console.WriteLine("ArrayEqual fault: second argument is null");
                }
                return false;
            }

            if (a1.Length != a2.Length)
            {
                if (console_out)
                {
                    Console.WriteLine("ArrayEqual fault: different lengths");
                    Console.WriteLine("  arg1 lenght = {0:D}", a1.Length);
                    Console.WriteLine("  arg2 lenght = {0:D}", a2.Length);
                }
                return false;
            }

            for (int i = 0; i < a1.Length; ++i)
                if (Math.Abs(a1[i] - a2[i]) > toleranse)
                {
                    if (console_out)
                    {
                        Console.WriteLine("ArrayEqual fault: different value at index {0:D}", i);
                        Console.WriteLine("  arg1[{0:D}]: {1:N}", i, a1[i]);
                        Console.WriteLine("  arg2[{0:D}]: {1:N}", i, a2[i]);
                    }
                    return false;
                }

            return true;
        }

        /// <summary>
        /// Compares arrays of complex
        /// </summary>
        /// <param name="a1">first array to compare</param>
        /// <param name="a2">second array to compare</param>
        /// <returns>true if arrays are equal, false in other case</returns>
        public static bool ArrayEqual(complex[] a1, complex[] a2)
        {
            if (a1 == null)
            {
                if (console_out)
                {
                    Console.WriteLine("ArrayEqual fault: first argument is null");
                }
                return false;
            }

            if (a2 == null)
            {
                if (console_out)
                {
                    Console.WriteLine("ArrayEqual fault: second argument is null");
                }
                return false;
            }

            if (a1.Length != a2.Length)
            {
                if (console_out)
                {
                    Console.WriteLine("ArrayEqual fault: different lengths");
                    Console.WriteLine("  arg1 lenght = {0:D}", a1.Length);
                    Console.WriteLine("  arg2 lenght = {0:D}", a2.Length);
                }
                return false;
            }

            for (int i = 0; i < a1.Length; ++i)
                if ((a1[i] - a2[i]).Abs() > toleranse)
                {
                    if (console_out)
                    {
                        Console.WriteLine("ArrayEqual fault: different value at index {0:D}", i);
                        Console.WriteLine("  arg1[{0:D}]: {1:N}", i, a1[i]);
                        Console.WriteLine("  arg2[{0:D}]: {1:N}", i, a2[i]);
                    }
                    return false;
                }

            return true;
        }
    }
}
